<!-- 文章列表页面 -->
<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a>文章管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 内容 -->
    <!-- 卡片组件 -->
    <el-card class="box-card">
      <!-- 表格组件 -->
      <!-- data:传入用于渲染表格内容的数组 ; prop:值为需要渲染的数据对应的属性名 ; label:表示渲染列的表头标题 -->
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        height="calc(100vh - 210px)"
      >
        <el-table-column width="50">
          <template v-slot="{ $index }">
            {{ $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column prop="type" label="类型" width="180">
          <template v-slot="{ row }">
            {{ row.type == 1 ? "文章" : "视频" }}
          </template>
        </el-table-column>

        <el-table-column prop="user.nickname" label="作者" width="250">
        </el-table-column>
        <el-table-column width="250">
          <template v-slot="{ row }">
            <el-button @click="editFn(row.id)">编辑</el-button>
            <el-button type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <!--  size-chaneg: 当pageSize改变时触发（每页展示的数据数量发送改变时触发）
             current-change：页码发送改变时触发（pageIndex改变时触发）
             current-page：绑定的是当前页码（也就是pageIndex）
             page-sizes：每页展示数量的选项集合
             page-size：每页展示的数据数量
             layout: 布局，展示的组件成员
             total：数据的总数量 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { articleList } from "@/api/article";
export default {
  data() {
    return {
      tableData: [],
      pageIndex: 1,
      pageSize: 5,
      total: 0,
    };
  },
  created() {
    this.getArticle();
  },
  methods: {
    //文章编辑
    editFn(id) {
      this.$router.push({
        path: "/layout/articleAdd",
        query: { id },
      });
    },
    getArticle() {
      //加载效果
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      articleList({ pageIndex: this.pageIndex, pageSize: this.pageSize }).then(
        (res) => {
          console.log(res);
          loading.close();
          this.tableData = res.data.data;
          this.total = res.data.total;
        }
      );
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.pageIndex = 1;
      this.getArticle();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageIndex = val;
      this.getArticle();
    },
  },
};
</script>
<style lang='less' scoped>
.el-card {
  margin-top: 20px;
}
</style>